﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrokerBase.Lib.Metadata;
using BrokerBase.Lib.Product;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Tool;
using Broker.View.Comm;
using Telerik.Windows.Controls;


namespace Broker.View.ProductView
{
    /// <summary>
    /// AuxPropertyForm.xaml 的交互逻辑
    /// </summary>
    public partial class AuxPropertyForm 
    {
        
        private IAuxProperty auxProp;
        private IAuxProperty original;

        void AuxPropertyForm_PreviewClosed(object sender, WindowPreviewClosedEventArgs e)
        {
            if (IsCommit)
            {
                if (Verify())
                {
                    BaseTools.CopyTo<IAuxProperty>(auxProp, original);
                    if (CallBack != null)
                        CallBack(original);
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        public AuxPropertyForm(IAuxProperty auxProp)
        {
            InitializeComponent();

            original = auxProp;

            this.auxProp = new AuxProperty(original.Owner);

            BaseTools.CopyTo<IAuxProperty>(auxProp, this.auxProp);

            Loaded += new RoutedEventHandler(AuxPropertyForm_Loaded);

            PreviewClosed += new EventHandler<WindowPreviewClosedEventArgs>(AuxPropertyForm_PreviewClosed);

            
        }

        void AuxPropertyForm_Loaded(object sender, RoutedEventArgs e)
        {
            cmbDataType.ItemsSource = LibGlobal.BaseDataType;
            DataContext = auxProp;

            if (auxProp.Owner.IsCheck)
            {
                btnConfirm.Visibility = Visibility.Collapsed;
                btnSel.Visibility = Visibility.Collapsed;
            }
        }

        public bool IsCommit { get; set; }

        public Action<IAuxProperty> CallBack { get; set; }

        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = true;
            Close();
            
        }

        private void btnSel_Click(object sender, RoutedEventArgs e)
        {
            IList<IAuxProperty> auxs = ProductUtil.LoadAuxPropertyTemplates();

            ListSelForm frm = new ListSelForm();

            GridViewDataColumn col1 = new GridViewDataColumn();
            col1.Header = "名称";
            col1.Width = new GridViewLength(160, GridViewLengthUnitType.Star);
            col1.DataMemberBinding = new Binding("Name");

            GridViewDataColumn col2 = new GridViewDataColumn();
            col2.Header = "类型";
            col2.Width = new GridViewLength(60, GridViewLengthUnitType.Star);
            col2.DataMemberBinding = new Binding("DataType");

            GridViewDataColumn col3 = new GridViewDataColumn();
            col3.Header = "值";
            col3.Width = new GridViewLength(120, GridViewLengthUnitType.Star);
            col3.DataMemberBinding = new Binding("Value");


            GridViewCheckBoxColumn col4 = new GridViewCheckBoxColumn();
            col4.Header = "是否可变";
            col4.Width = new GridViewLength(120, GridViewLengthUnitType.Star);
            col4.DataMemberBinding = new Binding("IsVariable");

            GridViewDataColumn col5 = new GridViewDataColumn();
            col5.Header = "描述";
            col5.Width = new GridViewLength(200, GridViewLengthUnitType.Star);
            col5.DataMemberBinding = new Binding("Description");

            frm.GridView.Columns.Add(col1);
            frm.GridView.Columns.Add(col2);
            frm.GridView.Columns.Add(col3);
            frm.GridView.Columns.Add(col4);
            frm.GridView.Columns.Add(col5);

            IAuxProperty d = frm.SelectByList<IAuxProperty>(auxs);

            if (d != null)
            {
                auxProp.Name = d.Name;
                auxProp.DataType = d.DataType;
                auxProp.Value = d.Value;
                auxProp.IsVariable = d.IsVariable;
                auxProp.Description = d.Description;

                DataContext = null;
                DataContext = auxProp;
            }
        }

        private bool Verify()
        {
            if (string.IsNullOrEmpty(auxProp.Name))
            {
                MessageBox.Show("请输入辅助属性名称", "提示");
                return false;
            }

            if (string.IsNullOrEmpty(auxProp.DataType))
            {
                MessageBox.Show("请输入辅助属性类型", "提示");
                return false;
            }

            if (string.IsNullOrEmpty(auxProp.Value))
            {
                MessageBox.Show("请输入辅助属性值", "提示");
                return false;
            }

            int v1=0;

            if (auxProp.DataType == "int" && !int.TryParse(auxProp.Value,out v1))
            {
                MessageBox.Show("你选择了整型类型，请输入整型值", "提示");
                return false;
            }

            double v2 = 0d;
            if (auxProp.DataType == "double" && !double.TryParse(auxProp.Value, out v2))
            {
                MessageBox.Show("你选择了小数类型，请输入小数值", "提示");
                return false;
            }

            bool v3 = false;
            if (auxProp.DataType == "bool" && !bool.TryParse(auxProp.Value, out v3))
            {
                MessageBox.Show("你选择了布尔类型，请输入布尔值", "提示");
                return false;
            }

            DateTime v4 = DateTime.Now;
            if (auxProp.DataType == "DateTime" && !DateTime.TryParse(auxProp.Value, out v4))
            {
                MessageBox.Show("你选择了日期类型，请输入日期值", "提示");
                return false;
            }

            return true;
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            IsCommit = false;
            Close();
        }
    }
}
